<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>路由</title>
    <script src="//cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>
    <script src="//unpkg.com/vue-router/dist/vue-router.js"></script>
</head>
<body>
    <div id="app">
        <a href="#/zhuye">主页</a>
        <a href="#/keji">科技</a>
        <a href="#/caijing">财经</a>
        <a href="#/yule">娱乐</a>

        <!-- 根据 :is 属性指定的组件名称,把对应的组件渲染到 component 标签所在的位置 -->
        <!-- 可以把 component 标签当做是 组件的占位符 -->
        <component :is="comName"></component>
    </div>

    <script type="text/javascript">
        const zhuye = {
            template: `<h1>主页信息</h1>`
        }

        const caijing = {
            template: `<h1>财经信息</h1>`
        }

        const keji = {
            template: `<h1>科技信息</h1>`
        }

        const yule = {
            template: `<h1>娱乐信息</h1>`
        }

        const vm = new Vue({
            el: '#app',
            data: {
                comName: 'zhuye'
            },
            components: {
                zhuye,
                keji,
                caijing,
                yule
            }
        })

        // 监听 window 的 onhashchange 事件,根据获取到的最新的 hash 值,切换要显示的组件的名称
        window.onhashchange = function () {
            // 通过 location.hash 获取到最新的 hash 值
            console.log(location.hash.slice(1))
            switch (location.hash.slice(1)) {
                case '/zhuye':
                    vm.comName = 'zhuye'
                    break
                case '/keji':
                    vm.comName = 'keji'
                    break
                case '/caijing':
                    vm.comName = 'caijing'
                    break
                case '/yule':
                    vm.comName = 'yule'
                    break
            }
        }
    </script>
</body>
</html>